import {
  Column,
  CreateDateColumn,
  Entity,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Sort } from '../../sort/entities/sort.entity';
import { Tag } from '../../tag/entities/tag.entity';
import { User } from '../../users/entities/user.entity';
import { Comment } from '../../comment/entities/comment.entity';
@Entity()
export class Article {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ length: 500, comment: '文章标题' })
  title: string;

  // @Column({ type: 'varchar', length: 24, comment: '作者' })
  // 多个文章一个作者
  @ManyToOne(() => User, (user) => user.article, { nullable: true })
  author: User;

  @Column({ type: 'varchar', comment: '封面' })
  cover?: string;

  @Column({ type: 'text', comment: '原始文章内容', nullable: false })
  raw_content: string;

  @Column({ type: 'text', comment: '渲染文章内容', nullable: false })
  render_content: string;

  @Column({ type: 'integer', comment: '喜爱数', default: 0 })
  likes: number;

  @Column({ type: 'integer', comment: '浏览量', default: 0 })
  views: number;

  // @Column({ type: 'integer', comment: '分类' })
  // 一个文章一个分类
  @ManyToOne(() => Sort, (sort) => sort.article, { nullable: true })
  sort?: Sort;

  // @Column({ type: 'simple-array', comment: '标签' })
  // 一个标签对应多个文章
  // 一个分类对应多个文章
  @ManyToMany(() => Tag, (tag) => tag.article, { nullable: true })
  tags?: Tag[];

  @Column({
    type: 'datetime',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '创建时间',
  })
  create_time: Date;

  @Column({
    type: 'datetime',
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
    comment: '更新时间',
  })
  update_time: Date;

  @Column({ type: 'boolean', comment: '是否禁用', default: false })
  is_disabled: boolean;

  @Column({ type: 'boolean', comment: '是否删除', default: false })
  is_deleted: boolean;

  @Column({ type: 'datetime', comment: '禁用时间', default: null })
  disabled_time: Date;

  @Column({ type: 'datetime', comment: '删除时间', default: null })
  delete_time: Date;

  // 一个文章对应多个评论
  @OneToMany(() => Comment, (comment) => comment.article)
  @JoinTable({
    name: 'article_comment', // 指定中间表的名称
    joinColumn: {
      name: 'articleId',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'commentId',
      referencedColumnName: 'id',
    },
  })
  comments: Comment[];
}
